package com.zbkj.admin.controller;

import com.zbkj.admin.service.AdminLoginService;
import com.zbkj.common.model.system.SystemInvoice;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.InvoiceRequest;
import com.zbkj.common.request.InvoiceSearchRequest;
import com.zbkj.common.request.InvoiceUpdateRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.InvoiceResponse;
import com.zbkj.common.response.SystemAdminResponse;
import com.zbkj.service.service.SystemInvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 电子发票
 */
@Slf4j
@RestController
@RequestMapping("api/admin/invoice")
@Api(tags = "电子发票") //配合swagger使用
public class SystemInvoiceController {

    @Autowired
    private SystemInvoiceService invoiceService;
    @Resource
    private AdminLoginService adminLoginService;

    /**
     * 开具电子发票，添加
     *
     * @param invoiceRequest |开票请求参数实体|必填
     */
    @ApiOperation(value = "开具电子发票")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<InvoiceResponse> getInvoiceByOrderId(@RequestBody @Validated InvoiceRequest invoiceRequest) {
        SystemAdminResponse currentAdmin = adminLoginService.getInfoByToken();//当前管理员登录用户，realName开票人
        InvoiceResponse invoiceResponse = invoiceService.getInvoiceByOrderId(invoiceRequest,currentAdmin.getRealName());
        return CommonResult.success(invoiceResponse);
    }

    //根据id获取发票详情
    @ApiOperation(value = "获取发票详情")
    @RequestMapping(value = "/id", method = RequestMethod.GET)
    public CommonResult<InvoiceResponse> invoiceInfoById(@RequestParam(value = "id") Integer id) {
        InvoiceResponse invoiceResponse = invoiceService.invoiceInfoById(id);
        return CommonResult.success(invoiceResponse);
    }

    /**
     * 修改电子发票
     * @param id |发票id|必填
     * @param request |修改参数|必填
     */
    @ApiOperation(value = "修改电子发票")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<InvoiceResponse> updateInvoice(@RequestParam Integer id,
                                                       @RequestBody @Validated InvoiceUpdateRequest request) {
        if (invoiceService.updateInvoice(id, request)) {
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除电子发票
     *
     * @param id |发票id|必填
     */
    @ApiOperation(value = "删除电子发票")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> deleteInvoice(@RequestParam(value = "id") Integer id) {
        if (invoiceService.deleteById(id)) {
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }

    /**
     * 电子发票列表
     *
     * @param request |搜索参数|必填
     * @param pageParamRequest |分页参数|必填
     */
    @ApiOperation(value = "电子发票列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<SystemInvoice>> invoiceList(@Validated InvoiceSearchRequest request,
                                                               @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(invoiceService.getInvoiceList(request, pageParamRequest));
    }

}
